package com.jqb.usermgr.manager;

import java.util.Date;

import org.hibernate.Session;

import com.jqb.usermgr.model.Log;
import com.jqb.usermgr.model.User;
import com.jqb.usermgr.util.HibernateUtil;

public class UserManagerImpl implements UserManager {

	
	public void addUser(User user) {
		Session session =null;
		try{
//			session = HibernateUtil.getSession();
			//会把session设置到当前线程中(ThreadLocal)
			session=HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.save(user);
			
			Log log=new Log();
			log.setTime(new Date());
			log.setType("安全日志");
			log.setDetail(user.getName()+"于"+log.getTime()+"进入系统");
			LogManager logManager=new LogManagerImpl();
			logManager.addLog(log);
			
			session.getTransaction().commit();
		}catch(Exception e){
			session.getTransaction().rollback();
			e.printStackTrace();
//		}finally{
			//执行完提交自动关闭,不需要手动来关
//			HibernateUtil.closeSession(session);
		}
		

	}

}
